package joodle.tools;

import java.awt.Graphics;
import java.awt.event.KeyEvent;

import joodle.domain.Canvas;
import joodle.domain.Point;
import joodle.domain.Shape;

/**
 * PointSelectionTool is responsible for coordinating the selection process by the user. 
 * PointSelectionTool class provides point selection in which user clicks on shapes he wants to 
 * select, perhaps holding the ctrl key.
 * <p><center>
 * <img src="http://ug.bcc.bilkent.edu.tr/~m_ozdal/Site/Reports_files/design_files/image010.jpg" />
 * </center></p>
 */
public class PointSelectionTool implements Tool {
	
	private static PointSelectionTool instance = new PointSelectionTool();
	private PointSelectionTool() {}
	
	public static PointSelectionTool instance() {
		return instance;
	}
	
	/**
	 * @see Tool#drawInterim(Graphics)
	 */
	@Override
	public void drawInterim(Graphics g) {
	}

	/**
	 * @see Tool#handleMove(Point, boolean, boolean)
	 */
	@Override
	public void handleMove(Point p, boolean ctrl, boolean lb) {
	}

	/**
	 * @see Tool#handlePress(Point, boolean)
	 */
	@Override
	public void handlePress(Point p, boolean ctrl) {
	}

	/**
	 * Invoked when a mouse button has been released on a component. Ctrl key plays an important role
	 * during the release event
	 * @see Tool#handleRelease(Point, boolean)
	 */
	@Override
	public void handleRelease(Point p, boolean ctrl) {
		Canvas canvas = Canvas.instance();
		Shape shape = canvas.getShapeByPoint(p);
		if(!ctrl)
			canvas.clearSelection();
		if(shape == null || shape.isSelected())
			return;
		canvas.setSelection(shape, true);
	}
	
	/**
	 * Invoked when escape key has been pressed.
	 * @see Tool#handleKeyPress(int)
	 */
	@Override
	public void handleKeyPress(int c) {
		if(c == KeyEvent.VK_ESCAPE) {
			Canvas.instance().clearSelection();
		}
	}

	/**
	 * @see Tool#handleKeyType(char)
	 */
	@Override
	public void handleKeyType(char c) {
	}

}
